﻿
@{
    ViewBag.Title = "Hub";
}

<h2>Hub</h2>
<span>姓名：</span>
<input type="text" id="name" name="name" value="老三" />
@*<input type="hidden" id="ClientName" value="@ViewBag.ClientName" />*@
<input type="text" id="msg" />
<input type="button" id="broadcast" value="广播" />
<br />
<h3>
    (<span id="MyClientName">@ViewBag.ClientName</span>):
</h3>
<ul id="messages"></ul>


@section scripts{
    <script src="~/Scripts/jquery-3.7.0.min.js"></script>
    <script src="~/Scripts/jquery.signalR-2.4.3.min.js"></script>
    <script src="~/signalr/hubchat"></script>

    <script>
        $(function () {
            



            var chat = $.connection.hubchat;  // 链接hub

            var myClientName = $('#name').val();  // 客户端输入的用户名

            // 定义供服务器端调用的方法
            chat.client.hello = function (message, clientName) {
                writeMsg('<b>' + clientName + '</b> 对大家说: '
                    + message, 'event-message');
            };
            $('#msg').focus();

            var clientName = $('#name').val();
            // 开始连接
            $.connection.hub.start().done(function () {
                $('#broadcast').click(function () {
                    // 调用send方法
                    chat.server.SendMsg($('#msg').val(), clientName);
                    $('#msg').val('').focus();
                });
            });

            function writeMsg(name, msg) {
                $('#messages').append(name + ":" + msg);
            }






        })
    </script>

}

